package com.dingreading.cloud.soms.service.impl;

import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.PageDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.soms.dto.StoreFunctionDto;
import com.dingreading.cloud.soms.entity.StoreFunction;
import com.dingreading.cloud.soms.entity.StoreFunctionSetting;
import com.dingreading.cloud.soms.entity.table.StoreFunctionTableDef;
import com.dingreading.cloud.soms.mapper.StoreFunctionMapper;
import com.dingreading.cloud.soms.service.StoreFunctionService;
import com.dingreading.cloud.soms.service.StoreFunctionSettingService;
import com.dingreading.cloud.soms.util.SomsEntityUtils;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 服务层实现。
 *
 * @author EDY
 * @since 2025-10-13
 */
@Service
public class StoreFunctionServiceImpl extends BaseServiceImpl<StoreFunctionMapper, StoreFunction, StoreFunctionTableDef> implements StoreFunctionService {

    @Override
    protected StoreFunctionTableDef getTable() {
        return StoreFunctionTableDef.storeFunction;
    }

    @Resource
    private StoreFunctionSettingService storeFunctionSettingService;

    @Override
    public R<Object> pageList(
            PageUtil pageUtil, String corpUid, String storeUid, String storeName, String staffUid, String staffName,
            String keyword, Integer status
    ) {
        if (StringUtils.isBlank(corpUid) || StringUtils.isBlank(storeUid) || StringUtils.isBlank(staffUid))
            return R.fail("门店参数错误！");

        QueryCondition condition = QueryCondition.createEmpty()
                .and(table.enabled.eq(1));

        if (StringUtils.isNotBlank(keyword))
            condition.and(table.name.like(keyword));
        if (null != status)
            condition.and(table.status.eq(status));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.desc());
        Page<StoreFunctionDto> page = getMapper().paginateAs(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper, StoreFunctionDto.class);
        List<StoreFunctionDto> records = page.getRecords();
        if (CollectionUtils.isNotEmpty(records)) {
            List<String> functionUids = records.stream().map(StoreFunctionDto::getUid).collect(Collectors.toList());
            Map<String, StoreFunctionSetting> storeFunctionSettingMap = storeFunctionSettingService.getByFunctionUidIn(storeUid, functionUids);
            for (StoreFunctionDto record : records) {
                StoreFunctionSetting storeFunctionSetting = storeFunctionSettingMap.get(record.getUid());
                if (storeFunctionSetting != null) {
                    record.setStatus(storeFunctionSetting.getStatus());
                    record.setLastStaffName(storeFunctionSetting.getLastStaffName());
                    record.setEditTime(storeFunctionSetting.getEditTime());
                }else {
                    record.setLastStaffName(null);
                    record.setEditTime(null);
                }
            }
        }
        PageDto<StoreFunctionDto> pageDto = new PageDto<>();
        pageDto.setTotal(page.getTotalRow());
        pageDto.setList(records);
        return R.ok(pageDto);
    }


    @Override
    public R<Object> updateStatus(String corpUid, String storeUid, String storeName, String staffUid, String staffName, String uid, Integer status) {
        if (StringUtils.isBlank(corpUid) || StringUtils.isBlank(storeUid) || StringUtils.isBlank(staffUid))
            return R.fail("门店参数错误！");
        if (StringUtils.isBlank(uid))
            return R.fail("请选择对应功能");
        if (status == null || status < 0 || status > 1)
            return R.fail("请选择功能状态");
        StoreFunction storeFunction = getMapper().selectOneByCondition(table.uid.eq(uid));
        if (storeFunction == null)
            return R.fail("该功能不存在");
        if (storeFunction.getEnabled() == 0)
            return R.fail("该功能已禁用");
        StoreFunctionSetting functionSetting = storeFunctionSettingService.getByUid(storeUid, uid);
        StoreFunctionSetting newFunctionSet = SomsEntityUtils.newStoreFunctionSetting(functionSetting, staffUid, staffName, storeUid, uid, status);
        boolean b = storeFunctionSettingService.saveOrUpdate(newFunctionSet);
        return b ? R.ok() : R.fail("更新失败！");
    }

    @Override
    public boolean getDegreeStatus(String storeUid, String identify) {

        StoreFunction storeFunction = getMapper().selectOneByCondition(table.identify.eq(identify));
        if (storeFunction == null)
            return false;
        if (storeFunction.getEnabled() == 0)
            return false;
        StoreFunctionSetting functionSetting = storeFunctionSettingService.getByUid(storeUid, storeFunction.getUid());
        if (functionSetting != null){
            storeFunction.setStatus(functionSetting.getStatus());
        }

        return storeFunction.getStatus() == 1 ? true : false;
    }
}
